//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI.WebControls;
using System.Text;

namespace CommunityServer.Controls
{
	public interface ITextEditor
	{
        string Text
        {
            get;
            set;
        }

        Unit Height { get; set;}
        Unit Width { get; set;}
        int Rows { get; set;}
        int Columns {get; set;}

		string StyleName {get;set;}

        //will allow us to control if HTML editing is enabled
        bool EnableHtmlModeEditing
        {
            get;
            set;
        }

        bool IsRichTextCapable
        {
            get;
        }

        string GetClientSideMethod { get;}
	}

    public class DefaultTextEditor : System.Web.UI.WebControls.TextBox, ITextEditor
    {
        public DefaultTextEditor():base()
        {
        }

        public string GetClientSideMethod
        {
            get { return "DefaultTextEditorClientText()";}   
        }

		//we can probably remove this
        public void Configure()
        {   
			if (this.Height != Unit.Empty)
			{
				this.Style["height"] = this.Height.ToString();
				this.Height = Unit.Empty;
			}
			else
				this.Style["height"] = "250px;";

			if (this.Width != Unit.Empty)
			{
				this.Style["width"] = this.Width.ToString();
				this.Width = Unit.Empty;
			}
			else
				this.Style["width"] = "100%";

            this.TextMode = TextBoxMode.MultiLine;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad (e);
			Configure();

        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender (e);
            this.Page.RegisterClientScriptBlock(this.ClientID,BuildClientScript());
            this.Page.RegisterClientScriptBlock("InsertText",RenderScript());
        }


        protected string BuildClientScript()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(@"<script type=""text/javascript"">{0}", Environment.NewLine);
            sb.AppendFormat(@"function {0} {{{1}", GetClientSideMethod, Environment.NewLine);
            sb.AppendFormat(@"return document.getElementById('{1}').value;", Environment.NewLine, this.ClientID);
            sb.AppendFormat(@"}}{0}", Environment.NewLine);
            sb.AppendFormat(@"</script>{0}", Environment.NewLine);
            return sb.ToString();
        }


        public bool IsRichTextCapable
        {
            get{return true;}
        }

        bool _isAdmin = false;

    	public string StyleName
    	{
    		get { throw new NotImplementedException(); }
    		set { throw new NotImplementedException(); }
    	}

    	public bool EnableHtmlModeEditing
        {
            get{return _isAdmin;}
            set{_isAdmin = value;}
        }



        protected string RenderScript()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=\"JavaScript\" type=\"text/javascript\">");
            string uk = this.UniqueID;//.Replace(":","_");
            sb.Append("function InsertText(textToInsert) {");
            
			sb.Append("var objPostEditor;");
			sb.Append("if(document.getElementById) {");			
			sb.Append("objPostEditor = document.getElementById(\"PostForm." + this.UniqueID + "\");");
//			sb.AppendFormat("PostForm.{0};",uk);
			sb.Append("} else {");
			sb.Append("objPostEditor = document.all[\"PostForm." + this.UniqueID + "\"];}");
			sb.Append("objPostEditor.value += textToInsert +\" \";");
			sb.Append("objPostEditor.focus();}");
            //sb.AppendFormat("document.PostForm.{0}.value += textToInsert +\" \";",uk);
            //sb.AppendFormat("document.PostForm.{0}.focus();",uk);
            sb.Append("</script>");

            return sb.ToString();
        }
    }
}

